package com.czy.thymeleafdemo.controller;

import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.io.File;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.DoubleToIntFunction;
import java.util.function.Predicate;

/**
 * @Description: TODO
 * @Author: chris_cai@enable-ets.com
 * @Since: 2018年06月06日
 */
public class TestDemo {

    @Test
    public void fn1() {
        System.out.println(Optional.ofNullable("abcd").filter(this::check).isPresent());
    }

    private boolean check(String s) {
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c == 'a') {
                return true;
            }
        }
        return false;
    }


    @Test
    public void fn2() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        System.out.println("输出奇数");
        eval(list,n -> n % 2 ==1);
    }

    public void eval(List<Integer> list , Predicate<Integer> predicate) {
        for (Integer n : list) {
            if (predicate.test(n)) {
                System.out.println(n);
            }
        }
    }

    @Test
    public void fn3() {
        calc(1,2.0,b-> (int) (b*0.3));
    }

    public void calc(Integer a,Double b, DoubleToIntFunction doubleToIntFunction) {
        int i = doubleToIntFunction.applyAsInt(b);
        if (a>i) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }

    @Test
    public void fn4() {
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(System.currentTimeMillis());
        String s = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime parse = LocalDateTime.parse("2018-11-16 10:00:59",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(parse);
        System.out.println(s);
    }

    @Test
    public void fn5() {
        Date date = new Date(System.currentTimeMillis());
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        Instant now = Instant.now(Clock.systemDefaultZone());
        System.out.println(now);
        System.out.println(date);
        System.out.println(localDateTime);
    }

    @Test
    public void fn7() {
        TemplateLocation location = new TemplateLocation("D:\\IdeaProject\\thymeleaf-demo\\target\\thymeleaf-demo-0.0.1\\templates\\");
        File f = new File(location.toString());
        System.out.println(location.toString());
        boolean exists = f.exists();
        System.out.println(exists);

    }
}
